package com.inforich.appManage.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.inforich.appManage.domain.Contact;
import com.inforich.appManage.service.IAppRoleService;
import com.inforich.appManage.service.IContactService;
import com.inforich.appManage.vo.AppUserVO;
import com.inforich.carManage.domain.Driver;
import com.inforich.carManage.service.IDriverService;
import com.inforich.common.constant.ProjectConstants;
import com.inforich.common.core.domain.AjaxResult;
import com.inforich.common.enums.RoleCodeEnum;
import com.inforich.company.domain.TransportCompany;
import com.inforich.company.service.ITransportCompanyService;
import com.inforich.orderManage.service.IBookingDriverDriversService;
import com.inforich.projectManage.domain.Project;
import com.inforich.projectManage.service.IProjectRubbishExpandService;
import com.inforich.projectManage.service.IProjectRubbishRemovalService;
import com.inforich.projectManage.service.IProjectRubbishReportService;
import com.inforich.projectManage.service.IProjectService;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.inforich.appManage.mapper.AppUserMapper;
import com.inforich.appManage.domain.AppUser;
import com.inforich.appManage.service.IAppUserService;
import org.springframework.transaction.annotation.Transactional;

/**
 * app用户Service业务层处理
 *
 * @author hjm
 * @date 2022-10-27
 */
@Service
public class AppUserServiceImpl extends ServiceImpl<AppUserMapper, AppUser> implements
    IAppUserService {

  @Autowired
  private IContactService contactService;
  @Autowired
  private IAppRoleService appRoleService;
  @Autowired
  private IProjectService projectService;
  @Autowired
  private IProjectRubbishExpandService projectRubbishExpandService;
  @Autowired
  private IProjectRubbishRemovalService projectRubbishRemovalService;
  @Autowired
  private IProjectRubbishReportService projectRubbishReportService;
  @Autowired
  private ITransportCompanyService transportCompanyService;
  @Autowired
  private IDriverService driverService;
  @Autowired
  private IBookingDriverDriversService bookingDriverDriversService;

  @Override
  public Page<AppUserVO> pageAppUserVO(Page<AppUserVO> page, Map<String, Object> map) {
    return baseMapper.pageAppUserVO(page, map);
  }

  @Override
  public List<AppUserVO> listAppUserVO(Map<String, Object> map) {
    return baseMapper.listAppUserVO(map);
  }

  @Override
  public Page<AppUser> pageAppUser(Page<AppUser> page, Map<String, Object> map) {
    return baseMapper.pageAppUser(page, map);
  }

  @Override
  public List<AppUser> listAppUser(Map<String, Object> map) {
    return baseMapper.listAppUser(map);
  }

  @Override
  public AppUser getByAccount(String account) {
    return getOne(new LambdaQueryWrapper<AppUser>()
        .eq(AppUser::getUserAccount, account));
  }

  @Override
  public AppUser getByIdnumber(String idnumber) {
    return this.getOne(new LambdaQueryWrapper<AppUser>()
        .eq(AppUser::getUserIdnumber, idnumber));
  }

  @Override
  public String getRoleCodeByUserId(String id) {
    Contact contact = contactService.getByAppUserId(id);
    String appRoleId = contact == null ? "" : contact.getAppRoleId();
    return StrUtil.isBlank(appRoleId) ? "" : appRoleService.getRoleCodeById(appRoleId);
  }

  @Override
  @Transactional(rollbackFor = Exception.class)
  public AjaxResult removeAppUser(String id) {
    Contact contact = contactService.getByAppUserId(id);
    // 没有关联数据直接删除
    if (contact == null) {
      removeById(id);
      return AjaxResult.success();
    }
    // 查看用户角色
    String appRoleId = contact.getAppRoleId();
    String roleCode = appRoleService.getRoleCodeById(appRoleId);
    // 项目管理员
    if (RoleCodeEnum.PROJECT_ADMIN.getRoleCode().equals(roleCode)) {
      return removeAppUserProject(id, contact.getProjectId());
    } else if (RoleCodeEnum.TRANSPORT_COMPANY_ADMIN.getRoleCode().equals(roleCode)) {
      return removeAppUserCompany(id, contact.getTransportCompanyId());
    } else if (RoleCodeEnum.DRIVER.getRoleCode().equals(roleCode)) {
      return removeAppUserDriver(id, contact.getDriverId());
    }
    return AjaxResult.success();
  }

  // 删除项目管理员及关联数据
  private AjaxResult removeAppUserProject(String id, String projectId) {
    Project project = projectService.getProjectById(projectId);
    if (project == null) {
      removeById(id);
      return AjaxResult.success();
    }
    if (projectService.checkProjectPass(project.getId())) {
      return AjaxResult.error("该用户关联的项目已审批通过，不可再删除");
    }
    removeById(id);
    contactService.removeByAppUserId(id);
    projectService.removeById(projectId);
    projectRubbishExpandService.removeByProjectId(projectId);
    projectRubbishRemovalService.removeByProjectId(projectId);
    projectRubbishReportService.removeByProjectId(projectId);
    return AjaxResult.success();
  }

  // 删除清运公司管理员及关联数据
  private AjaxResult removeAppUserCompany(String id, String companyId) {
    TransportCompany transportCompany = transportCompanyService.getTransportCompanyById(companyId);
    if (transportCompany == null) {
      removeById(id);
      return AjaxResult.success();
    }
    if (transportCompanyService.checkBind(companyId)) {
      return AjaxResult.error("该公司已被项目使用，不可再删除");
    }
    removeById(id);
    contactService.removeByCompanyId(companyId);
    transportCompanyService.removeById(companyId);
    driverService.removeByCompanyId(companyId);
    return AjaxResult.success();
  }

  // 删除司机及关联数据
  private AjaxResult removeAppUserDriver(String id, Long driverId) {
    Driver driver = driverService.getById(driverId);
    if (driver == null) {
      removeById(id);
      return AjaxResult.success();
    }
    // 该司机是否有运输单
    Boolean hasTransport = driverService.hasTransport(driverId);
    if (hasTransport) {
      return AjaxResult.error("该司机已分配运输单，不可再删除");
    }
    removeById(id);
    contactService.removeByAppUserId(id);
    driverService.removeById(driverId);
    bookingDriverDriversService.removeByDriverId(driverId);
    return AjaxResult.success();
  }

}
